package com.namo.cchapi.controller;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.namo.cchapi.service.MessageCaptchaService;
import com.namo.cchcommon.api.CommonResult;
import jdk.nashorn.internal.runtime.JSErrorType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WangXin
 * @date 2021/4/12 13:28
 */
@RestController
@RequestMapping("/message_captcha")
public class PhoneCaptchaController {
    @Autowired
    MessageCaptchaService messageCaptchaService;

    @Autowired
    Gson gson;

//    @RequestMapping(value = "/check_code",method = RequestMethod.POST)
//    public CommonResult checkMessageCode(@RequestBody String req){
//        JsonObject jsonObject = gson.fromJson(req,JsonObject.class);
//        JsonElement code = jsonObject.get("code").getAsString();
//
//    }

    @RequestMapping(value = "/get_code",method = RequestMethod.POST)
    public CommonResult getMessageCode(@RequestBody String req){
        JsonObject jsonObject = gson.fromJson(req,JsonObject.class);
        String phone = jsonObject.get("phone").getAsString();
        String s = messageCaptchaService.sendMessage(phone);
        return CommonResult.success("发送成功");
    }
    @RequestMapping(value = "/check",method = RequestMethod.POST)
    public CommonResult checkCode(@RequestBody String req){
        JsonObject jsonObject = gson.fromJson(req,JsonObject.class);
        String phone = jsonObject.get("phone").getAsString();
        String code = jsonObject.get("code").getAsString();
        String s = messageCaptchaService.checkMessage(phone,code);
        return CommonResult.success(s);
    }
}
